package com.meida.module.bus.provider.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BusMaterial;
import com.meida.module.bus.client.entity.BusMaterialView;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import com.meida.common.mybatis.model.*;

import java.io.File;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RestController;
import com.meida.common.springmvc.base.BaseController;
import com.meida.module.bus.client.entity.BusMasterAlldataView;
import com.meida.module.bus.provider.service.BusMasterAlldataViewService;


/**
 * VIEW控制器
 *
 * @author flyme
 * @date 2020-12-05
 */
@RestController
@RequestMapping("/bus/reportForMaster/")
public class BusMasterAlldataViewController extends BaseController<BusMasterAlldataViewService, BusMasterAlldataView> {

    @ApiOperation(value = "VIEW-分页列表", notes = "VIEW分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }

    @ApiOperation(value = "VIEW-列表", notes = "VIEW列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "VIEW-添加", notes = "添加VIEW")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
        return bizService.add(params);
    }

    @ApiOperation(value = "VIEW-更新", notes = "更新VIEW")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        return bizService.edit(params);
    }

    @ApiOperation(value = "VIEW-删除", notes = "删除VIEW")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return bizService.delete(params);
    }


    @ApiOperation(value = "VIEW-详情", notes = "VIEW详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }

    /**
     * 导出VIEW统计
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        params.remove("SIGN_TYPE");
        params.remove("APP_ID");
        params.remove("columns");
        params.remove("TIMESTAMP");
        params.remove("limit");
        params.remove("handlerName");
        params.remove("page");
        params.remove("sirt");
        params.remove("SIGN");
        params.remove("order");
        params.remove("sort");
        params.remove("NONCE");
//        bizService.export(params, request, response, "工程量清单", "工程量清单全数据");
        TemplateExportParams templateExportParams = new TemplateExportParams("excel/工程量清单全数据导出模板.xls", true);
        templateExportParams.setStyle(ExcelExportStylerDefaultImpl.class);
        Map<String, Object> reportBodyMap = Maps.newHashMap();
        List<Map<String, Object>> listMap = Lists.newArrayList();
        Map<String, Object> filterMap = Maps.newHashMap();
        params.forEach((key, val) -> {
            String filterKey = String.valueOf(key);
            if (filterKey.indexOf("param") < 0) {
                String setVal = String.valueOf(val);
                if (StrUtil.isNotEmpty(setVal)) {
                    filterMap.put(filterKey, val);
                }
            }
        });
        CriteriaQuery<BusMasterAlldataView> cq = new CriteriaQuery<BusMasterAlldataView>(BusMasterAlldataView.class);
        cq.select("bus_master_alldata_view.ver");
//        cq.createJoin(BusMaterialView.class).setMainField("materialId").setJoinField("materialId");
        cq.like(BusMasterAlldataView.class, "ver");
        cq.like(BusMasterAlldataView.class, "type");
        cq.like(BusMasterAlldataView.class, "dataKind");
        cq.like(BusMasterAlldataView.class, "instructNumber");
        cq.like(BusMasterAlldataView.class, "projectCode");
        cq.like(BusMasterAlldataView.class, "projectName");
        cq.like(BusMasterAlldataView.class, "majorName");
        cq.like(BusMasterAlldataView.class, "majorCode");
        cq.like(BusMasterAlldataView.class, "packageName");
        cq.like(BusMasterAlldataView.class, "packageNo");
        cq.like(BusMasterAlldataView.class, "materialWbsCode");
        cq.like(BusMasterAlldataView.class, "materialCode");
        cq.like(BusMasterAlldataView.class, "materialName");
        cq.like(BusMasterAlldataView.class, "materiaSpecs");
        cq.like(BusMasterAlldataView.class, "materialDesc");
        cq.like(BusMasterAlldataView.class, "technicalData");
        cq.like(BusMasterAlldataView.class, "serviceWbsCode");
        cq.like(BusMasterAlldataView.class, "serviceCode");
        cq.like(BusMasterAlldataView.class, "subjectName");
        cq.like(BusMasterAlldataView.class, "packageDesc");
        cq.like(BusMasterAlldataView.class, "levelNo");
        cq.like(BusMasterAlldataView.class, "contractDesc");
        cq.like(BusMasterAlldataView.class, "materialArea");
        cq.like(BusMasterAlldataView.class, "materialUse");
        cq.like(BusMasterAlldataView.class, "loss");
        cq.like(BusMasterAlldataView.class, "brandName");
        cq.like(BusMasterAlldataView.class, "remark");
        List<BusMasterAlldataView> excelDatas = bizService.getBusMasterAlldataViews(filterMap);
//        Collection<BusMasterAlldataView> excelDatas = bizService.listByMap(filterMap);
        excelDatas.stream().forEach(outputData -> {
            Map<String, Object> objectMap = new HashMap<>();
            int i = 1;
            // 版本
            objectMap.put("c" + i, outputData.getVer());
            i++;
            // 数据种类
            objectMap.put("c" + i, outputData.getDataKind().equals("0") ? "期初数据" : outputData.getDataKind().equals("1") ? "修正变更" : "追加变更");
            i++;
            // 版本种类
            objectMap.put("c" + i, outputData.getType());
            i++;
            // 指令号
            objectMap.put("c" + i, outputData.getInstructNumber());
            i++;
            // 项目编码
            objectMap.put("c" + i, outputData.getProjectCode());
            i++;
            // 项目名称
            objectMap.put("c" + i, outputData.getProjectName());
            i++;
            // 专业名称
            objectMap.put("c" + i, outputData.getMajorName());
            i++;
            // 专业代码
            objectMap.put("c" + i, outputData.getMajorCode());
            i++;
            // 工程包名称
            objectMap.put("c" + i, outputData.getPackageName());
            i++;
            // 工程包代码
            objectMap.put("c" + i, outputData.getPackageNo());
            i++;
            // 材料WBS代码
            objectMap.put("c" + i, outputData.getMaterialWbsCode());
            i++;
            // 全物料代码
            objectMap.put("c" + i, outputData.getMaterialCode());
            i++;
            // 设备、材料名称
            objectMap.put("c" + i, outputData.getMaterialName());
            i++;
            // 型号/规格
            objectMap.put("c" + i, outputData.getMateriaSpecs());
            i++;
            // 材料项目描述
            objectMap.put("c" + i, outputData.getMaterialDesc());
            i++;
            // 技术参数或技术说明
            objectMap.put("c" + i, outputData.getTechnicalData());
            i++;
            // 服务WBS代码
            objectMap.put("c" + i, outputData.getServiceWbsCode());
            i++;
            // 服务代码
            objectMap.put("c" + i, outputData.getServiceCode());
            i++;
            // 科目名称
            objectMap.put("c" + i, outputData.getSubjectName());
            i++;
            // 分承包发包项目描述
            objectMap.put("c" + i, outputData.getPackageDesc());
            i++;
            // 层级号
            objectMap.put("c" + i, outputData.getLevelNo());
            i++;
            // 对外合同清单描述
            objectMap.put("c" + i, outputData.getContractDesc());
            i++;
            // 分区域
            objectMap.put("c" + i, outputData.getMaterialArea());
            i++;
            // 分系统
            objectMap.put("c" + i, outputData.getMaterialUse());
            i++;
            // 合同清单工程量
            objectMap.put("c" + i, outputData.getContractBillQuantity());
            i++;
            // 标前工程量
            objectMap.put("c" + i, outputData.getBeforeMarkQuantity());
            i++;
            // 100预算成本工程量
            objectMap.put("c" + i, outputData.getCostQuantity100());
            i++;
            // 200预算成本工程量
            objectMap.put("c" + i, outputData.getCostQuantity200());
            i++;
            // 损耗
            objectMap.put("c" + i, outputData.getLoss());
            i++;
            // 计量单位
            objectMap.put("c" + i, outputData.getMeasurementUnit());
            i++;
            // 100采购计划量
            objectMap.put("c" + i, outputData.getUnitBillQuantity100());
            i++;
            // 200采购计划量
            objectMap.put("c" + i, outputData.getUnitBillQuantity200());
            i++;
            // 完成工程量_本月完成工程量
            objectMap.put("c" + i, outputData.getCurrentFinishMonthCount());
            i++;
            // 完成工程量_本月确认工程量
            objectMap.put("c" + i, outputData.getLastFinishMonthCount());
            i++;
            // 完成工程量_累计完成工程量
            objectMap.put("c" + i, outputData.getTotalFinishCount());
            i++;
            // 预算成本单价（未税）_单价_主材价
            objectMap.put("c" + i, outputData.getMainPrice());
            i++;
            // 预算成本单价（未税）_单价_安装价
            objectMap.put("c" + i, outputData.getInstallPrice());
            i++;
            // 预算成本单价（未税）_单价_综合单价
            objectMap.put("c" + i, outputData.getSyntheticalPrice());
            i++;
            // 预算成本单价（未税）_合价_主材费
            objectMap.put("c" + i, outputData.getMainAmount());
            i++;
            // 预算成本单价（未税）_合价_安装费
            objectMap.put("c" + i, outputData.getInstallAmount());
            i++;
            // 预算成本单价（未税）_合价_合计
            objectMap.put("c" + i, outputData.getSyntheticalAmount());
            i++;
            // 对外合同单价_有合同价（单价）_主材价
            objectMap.put("c" + i, outputData.getMainPrice1());
            i++;
            // 对外合同单价_有合同价（单价）_安装价
            objectMap.put("c" + i, outputData.getInstallPrice1());
            i++;
            // 对外合同单价_有合同价（单价）_综合单价
            objectMap.put("c" + i, outputData.getSyntheticalPrice1());
            i++;
            // 对外合同单价_拟合同价（单价）_主材价
            objectMap.put("c" + i, outputData.getMainPrice2());
            i++;
            // 对外合同单价_拟合同价（单价）_安装价
            objectMap.put("c" + i, outputData.getInstallPrice2());
            i++;
            // 对外合同单价_拟合同价（单价）_综合单价
            objectMap.put("c" + i, outputData.getSyntheticalPrice2());
            i++;
            // 对外合同单价_合同价（单价）_主材价
            objectMap.put("c" + i, outputData.getMainPrice3());
            i++;
            // 对外合同单价_合同价（单价）_安装价
            objectMap.put("c" + i, outputData.getInstallPrice3());
            i++;
            // 对外合同单价_合同价（单价）_综合单价
            objectMap.put("c" + i, outputData.getSyntheticalPrice3());
            i++;
            // 对外合同合价_有合同价（合价）_主材价
            objectMap.put("c" + i, outputData.getMainAmount1());
            i++;
            // 对外合同合价_有合同价（合价）_安装价
            objectMap.put("c" + i, outputData.getInstallAmount1());
            i++;
            // 对外合同合价_有合同价（合价）_综合单价
            objectMap.put("c" + i, outputData.getSyntheticalAmount1());
            i++;
            // 对外合同合价_拟合同价（合价）_主材价
            objectMap.put("c" + i, outputData.getMainAmount2());
            i++;
            // 对外合同合价_拟合同价（合价）_安装价
            objectMap.put("c" + i, outputData.getInstallAmount2());
            i++;
            // 对外合同合价_拟合同价（合价）_综合单价
            objectMap.put("c" + i, outputData.getSyntheticalAmount2());
            i++;
            // 对外合同合价_合同价（合价）_主材价
            objectMap.put("c" + i, outputData.getMainAmount3());
            i++;
            // 对外合同合价_合同价（合价）_安装价
            objectMap.put("c" + i, outputData.getInstallAmount3());
            i++;
            // 对外合同合价_合同价（合价）_综合单价
            objectMap.put("c" + i, outputData.getSyntheticalAmount3());
            i++;
            // 预估总产值_有合同价（合价）_主材价
            objectMap.put("c" + i, outputData.getMainsumPrice1());
            i++;
            // 预估总产值_有合同价（合价）_安装价
            objectMap.put("c" + i, outputData.getInstallsumPrice1());
            i++;
            // 预估总产值_有合同价（合价）_综合单价
            objectMap.put("c" + i, outputData.getContractsumSyntheticalPrice1());
            i++;
            // 预估总产值_拟合同价（合价）_主材价
            objectMap.put("c" + i, outputData.getMainsumPrice2());
            i++;
            // 预估总产值_拟合同价（合价）_安装价
            objectMap.put("c" + i, outputData.getInstallsumPrice2());
            i++;
            // 预估总产值_拟合同价（合价）_综合单价
            objectMap.put("c" + i, outputData.getContractsumSyntheticalPrice2());
            i++;
            // 预估总产值_合同价（合价）_主材价
            objectMap.put("c" + i, outputData.getMainsumPrice3());
            i++;
            // 预估总产值_合同价（合价）_安装价
            objectMap.put("c" + i, outputData.getInstallsumPrice3());
            i++;
            // 预估总产值_合同价（合价）_综合单价
            objectMap.put("c" + i, outputData.getContractsumSyntheticalPrice3());
            i++;
            // 本月实现产值（未税）_合同价（合价）_主材价
            objectMap.put("c" + i, outputData.getCurrentMonthmainPrice());
            i++;
            // 本月实现产值（未税）_合同价（合价）_安装价
            objectMap.put("c" + i, outputData.getCurrentMonthinstallPrice());
            i++;
            // 本月实现产值（未税）_合同价（合价）_合计
            objectMap.put("c" + i, outputData.getCurrentMonthsumPrice());
            i++;
            // 截止上月实现产值（未税）_合同价（合价）_主材价
            objectMap.put("c" + i, outputData.getLastMonthmainPrice());
            i++;
            // 截止上月实现产值（未税）_合同价（合价）_安装价
            objectMap.put("c" + i, outputData.getLastMonthinstallPrice());
            i++;
            // 截止上月实现产值（未税）_合同价（合价）_合计
            objectMap.put("c" + i, outputData.getLastMonthsumPrice());
            i++;
            // 累计实现产值（未税）_合同价（合价）_主材价
            objectMap.put("c" + i, outputData.getTotalMonthmainPrice());
            i++;
            // 累计实现产值（未税）_合同价（合价）_安装价
            objectMap.put("c" + i, outputData.getTotalMonthinstallPrice());
            i++;
            // 累计实现产值（未税）_合同价（合价）_合计
            objectMap.put("c" + i, outputData.getTotalsumPrice());
            i++;
            // 品牌
            objectMap.put("c" + i, outputData.getBrandName());
            i++;
            // 实现比例_本月实现比例（%）
            objectMap.put("c" + i, outputData.getRealizationRatioThisMonth());
            i++;
            // 实现比例_截至上月实现比例（%）
            objectMap.put("c" + i, outputData.getAsOfLastMonthsRealizationRatio());
            i++;
            // 实现比例_累计实现比例（%）
            objectMap.put("c" + i, outputData.getCumulativeRealizationRatio());
            i++;
            // 实现比例_本月实现金额
            objectMap.put("c" + i, outputData.getAmountRealizedThisMonth());
            i++;
            // 实现比例_截至上月实现金额
            objectMap.put("c" + i, outputData.getAmountRealizedAsOfLastMonth());
            i++;
            // 实现比例_累计实现金额
            objectMap.put("c" + i, outputData.getCumulativeRealizedAmount());
            i++;
            // 备注
            objectMap.put("c" + i, outputData.getRemark());
            listMap.add(objectMap);
        });
        reportBodyMap.put("maplist", listMap);
        Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, reportBodyMap);
//        File savefile = new File("D:/excel/");
//        if (!savefile.exists()) {
//            savefile.mkdirs();
//        }
        String fileName = "工程清单列表综合查询导出.xls";
        try {
            response.reset();
            response.setContentType("application/octet-stream;charset=UTF-8");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                + fileName);
            OutputStream fos = response.getOutputStream();
            workbook.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
